<template>

  <div>
    <el-form ref="form" :model="driverParams" label-width="80px">
      <el-form-item label="用户名称">
        <el-select v-model="driverParams.userId" v-if="userId!=''&&userId!=null" :disabled="true">
          <el-option label="请选择" value=""></el-option>
          <el-option v-for="(item,index) in globalUserData" :label="item.name" :value="item.id" :key="index"
          ></el-option>
        </el-select>


        <el-select v-model="driverParams.userId" v-if="userId==''||userId==null">
          <el-option label="请选择" value=""></el-option>
          <el-option v-for="(item,index) in globalUserData" :label="item.name" :value="item.id" :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车队">

        <el-select v-model="driverParams.fleetId" :disabled="true" v-if="userId!=''&&userId!=null">
          <el-option label="请选择" value=""></el-option>
          <el-option v-for="(item,index) in fleetData" :label="item.name" :value="item.id" :key="index"></el-option>
        </el-select>


        <el-select v-model="driverParams.fleetId" v-if="userId==''||userId==null">
          <el-option label="请选择" value=""></el-option>
          <el-option v-for="(item,index) in fleetData" :label="item.name" :value="item.id" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="driverParams.age" placeholder="请输入您的年龄" v-if="userId==''||userId==null"/>
        <el-input v-model="driverParams.age" placeholder="请输入您的年龄" :disabled="true" v-if="userId!=''&&userId!=null"/>
      </el-form-item>

      <el-form-item label="图片">
        <el-input v-model="driverParams.picture" placeholder="请选择图片" v-if="userId==''||userId==null"/>
        <el-input v-model="driverParams.picture" placeholder="请选择图片" :disabled="true" v-if="userId!=''&&userId!=null"/>
      </el-form-item>

      <el-form-item label="驾龄">
        <el-input v-model="driverParams.drivingAge" placeholder="请输入驾龄" v-if="userId==''||userId==null"/>
        <el-input v-model="driverParams.drivingAge" placeholder="请输入驾龄" :disabled="true"
                  v-if="userId!=''&&userId!=null"
        />
      </el-form-item>

    </el-form>

    <el-button type="info">
      <router-link :to="'/driver-index'">
        取消
      </router-link>
    </el-button>
    <el-button type="primary" v-if="userId==''||userId==null" @click="saveDriver">添加</el-button>

  </div>


</template>

<script>
import driver from '@/api/base/driver/index'
import globalUser from '@/api/base/globalUser/index'
import fleet from '@/api/base/fleet/index'

export default {
  name: 'add',
  data() {
    return {
      driverParams: {
        userId: '',
        fleetId: '',
        age: '',
        picture: '',
        drivingAge: ''
      },
      globalUserData: [],
      fleetData: [],
      userId: ''
    }
  },
  methods: {
    findOneDriver(id) {
      driver.findOneDriver(id).then(data => {
        console.log(data)
        this.driverParams = data.data
      })
    },
    queryAll() {
      globalUser.queryAll().then(data => {
        this.globalUserData = data.data
      })
    },
    findAll() {
      fleet.findAll().then(data => {
        this.fleetData = data.data
      })
    },
    saveDriver(){
      driver.saveDriver(this.driverParams).then(data =>{
        console.log(data)
          this.$message({
            type:'success',
            message:'添加成功!',
            showClose:true
          })
        this.$router.push({path:'/driver-index'})
      })
    }
  }, mounted() {
    if (this.$route.params && this.$route.params.id) {
      this.findOneDriver(this.$route.params.id)
      this.userId = this.$route.params.id
    }
    this.queryAll()
    this.findAll()
  }
}
</script>

<style scoped>

</style>
